package com.lonely.common.threadlocals;

import com.lonely.common.enums.DataScope;

/**
 * @author: 黄志标
 * @date: 2025/6/20 15:28
 * @description: 数据权限线程变量
 */
public class DataPermissionThreadLocal {


    /**
     * 数据权限
     */
    public static final ThreadLocal<DataScope> DATA_SCOPE_THREAD_LOCAL = ThreadLocal.withInitial(() -> DataScope.ALL);

    /**
     * 左表达式
     */
    public static final ThreadLocal<String> LEFT_EXPRESS_THREAD_LOCAL = ThreadLocal.withInitial(() -> "");


    /**
     * 设置数据权限
     *
     * @param dataScope
     * @param leftExpress
     */
    public static void setDataPermission(DataScope dataScope, String leftExpress) {
        setScope(dataScope);
        setLeftExpress(leftExpress);
    }

    /**
     * 设置数据权限
     *
     * @param dataScope
     */
    public static void setScope(DataScope dataScope) {
        DATA_SCOPE_THREAD_LOCAL.set(dataScope);
    }


    /**
     * 获取数据权限
     *
     * @return
     */
    public static DataScope getScope() {
        return DATA_SCOPE_THREAD_LOCAL.get();
    }

    /**
     * 设置左表达式
     *
     * @param leftExpress
     */
    public static void setLeftExpress(String leftExpress) {
        LEFT_EXPRESS_THREAD_LOCAL.set(leftExpress);
    }

    /**
     * 获取左表达式
     *
     * @return
     */
    public static String getLeftExpress() {
        return LEFT_EXPRESS_THREAD_LOCAL.get();
    }

    /**
     * 清除线程变量
     */
    public static void clear() {
        DATA_SCOPE_THREAD_LOCAL.remove();
        LEFT_EXPRESS_THREAD_LOCAL.remove();
    }

}
